package com.wangzaiplus.test.juc.thread;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @ClassName SynDemo1
 * @Description: TODO
 * @Author wgc
 * @Date 2020/06/28 18:05
 * @Version V1.0
 **/
public class ReentrantLockDemo2 {
    private static int num=0;
    private  static ReentrantLock  fairLock =new ReentrantLock (false);


    public static class T1 extends  Thread{
        public T1(String  name){
            super(name);
        }
        @Override
        public void run() {
            for (int i = 0; i <5; i++) {
                fairLock.lock();
                try{
                    System.out.println(this.getName()+"获取锁");
                }finally {
                    fairLock.unlock();
                }

            }
        }
    }
    public static void main(String[] args) throws InterruptedException {

            T1 t1 = new T1("t1");
            T1 t2 = new T1("t2");
            T1 t3 = new T1("t3");
            t1.start();
            t2.start();
            t3.start();
            //等待3个线程结束打印num
            t1.join();
            t2.join();
            t3.join();

        System.out.println(ReentrantLockDemo2.num);

    }
}
